package com.imay.base.util;

import android.animation.ObjectAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.webkit.WebView;
import android.widget.AbsListView;
import android.widget.ScrollView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

/**
 * Created by jeaman on 2017/2/15.
 */

public class RecyclerViewUtil {
    private static int gridLayoutManagerPosition = 51;
    private static int linearLayoutManagerPosition = 11;
    private static boolean callRefresh;

    public static int getRecyclerFirstVisibleItemPosition(RecyclerView recyclerView) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager staggeredGridLayoutManager = ((StaggeredGridLayoutManager) layoutManager);
            return findMin(staggeredGridLayoutManager.findFirstVisibleItemPositions(null));
        }
        return 0;
    }

    private static int findMin(int[] lastPositions) {
        int min = lastPositions[0];
        for (int value : lastPositions) {
            if (value < min) {
                min = value;
            }
        }
        return min;
    }


    /**
     * 可滚动view
     *
     * @param mSmoothToTopView
     */
    public static void smoothScrollToTop(View mSmoothToTopView) {
        if (mSmoothToTopView != null) {
            if (mSmoothToTopView instanceof AbsListView) {
                AbsListView smoothToTopView = (AbsListView) mSmoothToTopView;
                int firstVisiblePosition = smoothToTopView.getFirstVisiblePosition();
                if (firstVisiblePosition < 11) {
                    smoothToTopView.smoothScrollToPosition(0);
                } else {
                    //太多item时加快速度
                    smoothToTopView.setSelection(10);
                    smoothToTopView.smoothScrollToPosition(0);
                }
                smoothToTopView.clearFocus();
            } else if (mSmoothToTopView instanceof RecyclerView) {
                RecyclerView smoothToTopView = (RecyclerView) mSmoothToTopView;
                RecyclerViewUtil.smoothScrollToTop(smoothToTopView);
            } else if (mSmoothToTopView instanceof ScrollView) {
                ScrollView scrollView = ((ScrollView) mSmoothToTopView);
                scrollView.smoothScrollTo(scrollView.getScrollX(), 0);
            } else if (mSmoothToTopView instanceof WebView) {
                ObjectAnimator anim = ObjectAnimator.ofInt(mSmoothToTopView, "scrollY",
                        mSmoothToTopView.getScrollY(), 0);
                anim.setDuration(200).start();
            }
        }
    }

    /**
     * 滚动到顶部
     *
     * @param mRecyclerView
     */
    public static void smoothScrollToTop(RecyclerView mRecyclerView) {
        if (mRecyclerView == null) {
            return;
        }
        int firstVisiblePosition = RecyclerViewUtil.getRecyclerFirstVisibleItemPosition(mRecyclerView);
        RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if (firstVisiblePosition < gridLayoutManagerPosition) {
                mRecyclerView.smoothScrollToPosition(0);
            } else {
                //太多item时加快速度
                mRecyclerView.scrollToPosition(gridLayoutManagerPosition - 1);
                mRecyclerView.smoothScrollToPosition(0);
            }
        } else if (layoutManager instanceof LinearLayoutManager) {
            if (firstVisiblePosition < linearLayoutManagerPosition) {
                mRecyclerView.smoothScrollToPosition(0);
            } else {
                //太多item时加快速度
                mRecyclerView.scrollToPosition(linearLayoutManagerPosition - 1);
                mRecyclerView.smoothScrollToPosition(0);
            }
        }
        mRecyclerView.clearFocus();
    }

    /**
     * 滚动到顶部刷新布局
     *
     * @param mRecyclerView
     * @param mRefreshLayout
     */
    public static void smoothScrollToTopWithRefresh(final RecyclerView mRecyclerView, final TwinklingRefreshLayout mRefreshLayout) {
        if (mRecyclerView == null || mRefreshLayout == null) {
            return;
        }
        callRefresh = true;
        int position = getRecyclerFirstVisibleItemPosition(mRecyclerView);
        View view = mRecyclerView.getLayoutManager().findViewByPosition(position);
        if (view != null && position == 0 && view.getTop() >= 0) {
            if (callRefresh) {
                callRefresh = false;
                mRefreshLayout.startRefresh();
            }
        } else {
            smoothScrollToTop(mRecyclerView);
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        int position = getRecyclerFirstVisibleItemPosition(mRecyclerView);
                        if (position != 0) {
                            callRefresh = false;
                        }
                    }
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    if (callRefresh) {
                        int position = -1;
                        position = getRecyclerFirstVisibleItemPosition(mRecyclerView);
                        View viewHead = null;
                        if (mRecyclerView.getAdapter() instanceof BaseQuickAdapter) {
                            BaseQuickAdapter adapter = (BaseQuickAdapter) mRecyclerView.getAdapter();
                            viewHead = adapter.getHeaderLayout();
                        }
                        if (viewHead != null) {
                            if (position == 0 && viewHead.getTop() >= 0) {
                                callRefresh = false;
                                mRefreshLayout.startRefresh();
                            }
                        } else {
                            View view = mRecyclerView.getLayoutManager().findViewByPosition(position);
                            if (view != null && position == 0 && view.getTop() >= 0) {
                                callRefresh = false;
                                mRefreshLayout.startRefresh();
                            }
                        }
                    }
                }
            });
        }
    }
}
